package com.gopay.bis.cmbc.expay.operation;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;

import com.gopay.bis.cmbc.expay.integrate.Letter;
import com.gopay.bis.cmbc.expay.integrate.PostOffice;
import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.Result;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.cmbc.expay.CmbcExPayMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.NamedThreadFactory;
import com.gopay.remote.bank.BisExPayRevBankRemoteService;

public class OtherBankReceiveBean implements InitializingBean {
    private static final Logger logger = LoggerFactory.getLogger(OtherBankReceiveBean.class);

    private PostOffice postOffice;
    private CmbcAdPayParseMessage cmbcAdPayParseMessage;
    @Resource(name = "cmbcNotifyClient")
    private BisExPayRevBankRemoteService cmbcNotifyClient;

    private int resPoolSize = 5;// 线程池
    // 初始化线程池
    private ExecutorService executors = Executors.newFixedThreadPool(resPoolSize, new NamedThreadFactory(
            "OtherBankReceiveBean"));

    @Override
    public void afterPropertiesSet() throws Exception {
        logger.info("OtherBankReceiveBean receive message start: ");

        for (int i = 0; i < resPoolSize; i++) {
            executors.execute(new Runnable() {
                Result result = new Result(false);

                @Override
                public void run() {
                    while (true) {
                        logger.info("OtherBankReceiveBean takeFromReceiveQueue start: ");
                        try {
                            Letter letter = postOffice.takeFromReceiveQueue();
                            byte[] resMsg = letter.getData(); // 银行结果通知
                            CmbcExPayMessage message = new CmbcExPayMessage(); // 通知bis对象
                            message = cmbcAdPayParseMessage.parseCrossAdPayResp(message, resMsg);
                            result.setModel("res", message);
                            result.setSuccess(true);
                            if(!"000000".equals(message.getRetCode())){
                                //add by liuyu, 20170803, 快捷API失败通知
                                result.setBankRetCode(message.getRetCode());
                            }
                            // 发起通知bis
                            cmbcNotifyClient.revBankResult(result);
                            // if (dataContainer == null) {
                            // continue;
                            // }
                        } catch (GopayException ex) {
                            BankLogUtils.printServiceImplExceptionLog(ex);
                            result.setResultCode(ex.getErrCode());
                            cmbcNotifyClient.revBankResult(result);
                        } catch (Exception e) {
                            logger.error("MessageReceiveBean error: ", e);
                            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6057);
                            cmbcNotifyClient.revBankResult(result);
                        }
                    }
                }
            });
        }
        // executors.shutdown();
    }

    public void setPostOffice(PostOffice postOffice) {
        this.postOffice = postOffice;
    }

    public void setCmbcAdPayParseMessage(CmbcAdPayParseMessage cmbcAdPayParseMessage) {
        this.cmbcAdPayParseMessage = cmbcAdPayParseMessage;
    }

    public void setCmbcNotifyClient(BisExPayRevBankRemoteService cmbcNotifyClient) {
        this.cmbcNotifyClient = cmbcNotifyClient;
    }

}
